package com.sidneysimmons.financially.fragments;

import java.io.Serializable;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.sidneysimmons.financially.Category;
import com.sidneysimmons.financially.R;
import com.sidneysimmons.financially.activities.AccountActivity;

public class EditCategoryDialogFragment extends DialogFragment {
	
	/** Used by the FragmentManager to identify this fragment. */
	public static String TAG = "edit_category_dialog";
	
	private View editCategoryView;
	private Category originalCategory;
	
	public static EditCategoryDialogFragment newInstance(Category originalCategory) {
		Bundle bundle = new Bundle();
        bundle.putSerializable("originalCategory", originalCategory);
		
		EditCategoryDialogFragment fragment = new EditCategoryDialogFragment();
        fragment.setArguments(bundle);
        fragment.setOriginalCategory(originalCategory);
    	
        return fragment;
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		// If the bundle is not null and category is null, restore the category
		// If the bundle is null, the category should already be retained in this object
		if (savedInstanceState != null && originalCategory == null) {
			Serializable serialCategory = savedInstanceState.getSerializable("originalCategory");
			if (serialCategory != null) {
				originalCategory = (Category) serialCategory;
			}
		}

		// Inflate the view to use
		Context context = getActivity();
		LayoutInflater inflater = LayoutInflater.from(context);
		editCategoryView = inflater.inflate(R.layout.edit_category_dialog, null);
		
		// Populate the view with the existing category data
		populateExistingCategoryView(editCategoryView);

        return new AlertDialog.Builder(context)
        		.setView(editCategoryView)
                .setPositiveButton(R.string.edit_category_dialog_text_save,
                    new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							// populate Category and send back to UI
							AccountActivity activity = (AccountActivity) getActivity();
							activity.validateAndEditCategory(activity.getAccountInFocus(), originalCategory, populateUpdatedCategory(editCategoryView));
						}
                    }
                )
                .setNegativeButton(R.string.edit_category_dialog_text_delete,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                        	// Delete the category
                        	AccountActivity activity = (AccountActivity) getActivity();
                        	activity.deleteCategory(activity.getAccountInFocus(), originalCategory);
                        }
                    }
                )
                .create();
    }
	
	/**
	 * Fill in the fields on the dialog with the existing category information.
	 * 
	 * @param editCategoryView - the View to get the data from
	 */
	protected void populateExistingCategoryView(View editCategoryView) {
		// Get fields here
		EditText categoryNameField = (EditText) editCategoryView.findViewById(R.id.edit_category_dialog_edittext_category_name);

		// Set the data here
		categoryNameField.setText(originalCategory.getTitle());
	}
	
	/**
     * Get the newly entered data from the fields on the dialog.
     * 
     * @param editCategoryView - the View to get the data from
     * @return a populated Category object
     */
    protected Category populateUpdatedCategory(View editCategoryView) {
    	Category category = new Category();
    	
    	// Get fields here
    	EditText categoryNameField = (EditText) editCategoryView.findViewById(R.id.edit_category_dialog_edittext_category_name);
    	
    	// Set data here
    	category.setTitle(categoryNameField.getText().toString().trim());
    	
    	return category;
    }
    
    /**
	 * Use this to save any data right before the Fragment is destroyed.
	 */
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		
		outState.putSerializable("originalCategory", originalCategory);
	}
	
	/**
	 * Set the Category object.
	 */
	public void setOriginalCategory(Category originalCategory) {
		this.originalCategory = originalCategory;
	}
	
	/**
	 * Gets the Category object.
	 * 
	 * @return Category
	 */
	public Category getOriginalCategory() {
		return originalCategory;
	}
}